home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / ov-cx-mat.cc < prev    next >
C/C++ Source or Header  |  1996-11-07  |  7KB  |  318 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "lo-ieee.h"
  32. #include "mx-base.h"
  33.  
  34. #include "gripes.h"
  35. #include "oct-obj.h"
  36. #include "ops.h"
  37. #include "ov-complex.h"
  38. #include "ov-cx-mat.h"
  39. #include "ov-re-mat.h"
  40. #include "ov-scalar.h"
  41. #include "pr-output.h"
  42.  
  43. octave_allocator
  44. octave_complex_matrix::allocator (sizeof (octave_complex_matrix));
  45.  
  46. int
  47. octave_complex_matrix::t_id (-1);
  48.  
  49. const string
  50. octave_complex_matrix::t_name ("complex matrix");
  51.  
  52. octave_complex_matrix::octave_complex_matrix (const ComplexRowVector& v,
  53.                           int pcv)
  54.   : octave_base_value (),
  55.     matrix ((pcv < 0 && Vprefer_column_vectors) || pcv
  56.         ? ComplexMatrix (v.transpose ()) : ComplexMatrix (v)) { }
  57.  
  58. octave_complex_matrix::octave_complex_matrix (const ComplexColumnVector& v,
  59.                           int pcv)
  60.   : octave_base_value (),
  61.     matrix ((pcv < 0 && Vprefer_column_vectors) || pcv
  62.         ? ComplexMatrix (v) : ComplexMatrix (v.transpose ())) { }
  63.  
  64. octave_value *
  65. octave_complex_matrix::try_narrowing_conversion (void)
  66. {
  67.   octave_value *retval = 0;
  68.  
  69.   int nr = matrix.rows ();
  70.   int nc = matrix.cols ();
  71.  
  72.   if (nr == 1 && nc == 1)
  73.     {
  74.       Complex c = matrix (0, 0);
  75.  
  76.       if (imag (c) == 0.0)
  77.     retval = new octave_scalar (::real (c));
  78.       else
  79.     retval = new octave_complex (c);
  80.     }
  81.   else if (nr == 0 && nc == 0)
  82.     retval = new octave_matrix (Matrix ());
  83.   else if (matrix.all_elements_are_real ())
  84.     retval = new octave_matrix (::real (matrix));
  85.  
  86.   return retval;
  87. }
  88.  
  89. octave_value
  90. octave_complex_matrix::index (const octave_value_list& idx) const
  91. {
  92.   octave_value retval;
  93.  
  94.   int len = idx.length ();
  95.  
  96.   switch (len)
  97.     {
  98.     case 2:
  99.       {
  100.     idx_vector i = idx (0).index_vector ();
  101.     idx_vector j = idx (1).index_vector ();
  102.  
  103.     retval = ComplexMatrix (matrix.index (i, j));
  104.       }
  105.       break;
  106.  
  107.     case 1:
  108.       {
  109.     idx_vector i = idx (0).index_vector ();
  110.  
  111.     retval = ComplexMatrix (matrix.index (i));
  112.       }
  113.       break;
  114.  
  115.     default:
  116.       error ("invalid number of indices (%d) for complex matrix value", len);
  117.       break;
  118.     }
  119.  
  120.   return retval;
  121. }
  122.  
  123. extern void assign (Array2<Complex>&, const Array2<Complex>&);
  124.  
  125. void
  126. octave_complex_matrix::assign (const octave_value_list& idx,
  127.                    const ComplexMatrix& rhs)
  128. {
  129.   int len = idx.length ();
  130.  
  131.   switch (len)
  132.     {
  133.     case 2:
  134.       {
  135.     idx_vector i = idx (0).index_vector ();
  136.     idx_vector j = idx (1).index_vector ();
  137.  
  138.     matrix.set_index (i);
  139.     matrix.set_index (j);
  140.  
  141.     ::assign (matrix, rhs);
  142.       }
  143.       break;
  144.  
  145.     case 1:
  146.       {
  147.     idx_vector i = idx (0).index_vector ();
  148.  
  149.     matrix.set_index (i);
  150.  
  151.     ::assign (matrix, rhs);
  152.       }
  153.       break;
  154.  
  155.     default:
  156.       error ("invalid number of indices (%d) for indexed matrix assignment",
  157.          len);
  158.       break;
  159.     }
  160. }
  161.  
  162. extern void assign (Array2<Complex>&, const Array2<double>&);
  163.  
  164. void
  165. octave_complex_matrix::assign (const octave_value_list& idx,
  166.                    const Matrix& rhs)
  167. {
  168.   int len = idx.length ();
  169.  
  170.   switch (len)
  171.     {
  172.     case 2:
  173.       {
  174.     idx_vector i = idx (0).index_vector ();
  175.     idx_vector j = idx (1).index_vector ();
  176.  
  177.     matrix.set_index (i);
  178.     matrix.set_index (j);
  179.  
  180.     ::assign (matrix, rhs);
  181.       }
  182.       break;
  183.  
  184.     case 1:
  185.       {
  186.     idx_vector i = idx (0).index_vector ();
  187.  
  188.     matrix.set_index (i);
  189.  
  190.     ::assign (matrix, rhs);
  191.       }
  192.       break;
  193.  
  194.     default:
  195.       error ("invalid number of indices (%d) for indexed matrix assignment",
  196.          len);
  197.       break;
  198.     }
  199. }
  200.  
  201. bool
  202. octave_complex_matrix::valid_as_scalar_index (void) const
  203. {
  204.   // XXX FIXME XXX
  205.   return false;
  206. }
  207.  
  208. bool
  209. octave_complex_matrix::valid_as_zero_index (void) const
  210. {
  211.   // XXX FIXME XXX
  212.   return false;
  213. }
  214.  
  215. bool
  216. octave_complex_matrix::is_true (void) const
  217. {
  218.   bool retval = false;
  219.  
  220.   if (rows () == 0 || columns () == 0)
  221.     {
  222.       int flag = Vpropagate_empty_matrices;
  223.  
  224.       if (flag < 0)
  225.     warning ("empty matrix used in conditional expression");
  226.       else if (flag == 0)
  227.     error ("empty matrix used in conditional expression");
  228.     }
  229.   else
  230.     {
  231.       Matrix m = (matrix.all ()) . all ();
  232.  
  233.       retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0);
  234.     }
  235.  
  236.   return retval;
  237. }
  238.  
  239. double
  240. octave_complex_matrix::double_value (bool force_conversion) const
  241. {
  242.   double retval = octave_NaN;
  243.  
  244.   int flag = force_conversion;
  245.  
  246.   if (! flag)
  247.     flag = Vok_to_lose_imaginary_part;
  248.  
  249.   if (flag < 0)
  250.     gripe_implicit_conversion ("complex matrix", "real scalar");
  251.  
  252.   if (flag)
  253.     {
  254.       if ((rows () == 1 && columns () == 1)
  255.       || (Vdo_fortran_indexing && rows () > 0 && columns () > 0))
  256.     retval = ::real (matrix (0, 0));
  257.       else
  258.     gripe_invalid_conversion ("complex matrix", "real scalar");
  259.     }
  260.   else
  261.     gripe_invalid_conversion ("complex matrix", "real scalar");
  262.  
  263.   return retval;
  264. }
  265.  
  266. Matrix
  267. octave_complex_matrix::matrix_value (bool force_conversion) const
  268. {
  269.   Matrix retval;
  270.  
  271.   int flag = force_conversion;
  272.  
  273.   if (! flag)
  274.     flag = Vok_to_lose_imaginary_part;
  275.  
  276.   if (flag < 0)
  277.     gripe_implicit_conversion ("complex matrix", "real matrix");
  278.  
  279.   if (flag)
  280.     retval = ::real (matrix);
  281.   else
  282.     gripe_invalid_conversion ("complex matrix", "real matrix");
  283.  
  284.   return retval;
  285. }
  286.  
  287. Complex
  288. octave_complex_matrix::complex_value (bool) const
  289. {
  290.   Complex retval (octave_NaN, octave_NaN);
  291.  
  292.   if ((rows () == 1 && columns () == 1)
  293.       || (Vdo_fortran_indexing && rows () > 0 && columns () > 0))
  294.     retval = matrix (0, 0);
  295.   else
  296.     gripe_invalid_conversion ("complex matrix", "complex scalar");
  297.  
  298.   return retval;
  299. }
  300.  
  301. ComplexMatrix
  302. octave_complex_matrix::complex_matrix_value (bool) const
  303. {
  304.   return matrix;
  305. }
  306.  
  307. void
  308. octave_complex_matrix::print (ostream& os, bool pr_as_read_syntax)
  309. {
  310.   octave_print_internal (os, matrix, pr_as_read_syntax, struct_indent);
  311. }
  312.  
  313. /*
  314. ;;; Local Variables: ***
  315. ;;; mode: C++ ***
  316. ;;; End: ***
  317. */
  318.